<template>
	<view class="app">
		<view class="panel">
			<view class="login">
				<view class="h1">
					客服 - 手机登录
				</view>		
				<view class="ipt">
					<input type="number" v-model="phone" placeholder="输入手机号" />
				</view>
				<view class="ipt">
					<input type="number" v-model="code" placeholder="输入验证码" />
					<text @tap.stop="sendMsg">{{ msg }}</text>
				</view>
				<view class="btn" @tap.stop="submit">
					登录
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var that,App=getApp(),Comm=getApp().comm;
	var clear;
	
	export default {
		data() {
			return {
				phone: '',
				code: '',
				msg: '获取验证码',
			}
		},
		onLoad() {
			that=this;
		},
		methods: {
			sendMsg() {
				if (this.readonly) {
					return;
				}
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^1([3-9])\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					});
					return;
				}
				Comm.http('/api/user/smsCode',{mobile:this.phone},'POST').then(res=>{
					if(res.code==1){
						this.getCodeState();
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg
						});
					}
				});
			},
			//验证码按钮文字状态
			getCodeState() {
				clear && clearInterval(clear);
				const _this = this;
				this.readonly = true;
				this.msg = '60S';
				var s = 60;
				clear = setInterval(() => {
					s--;
					_this.msg = s + 'S';
					if (s <= 0) {
						clearInterval(clear);
						_this.msg = '获取验证码';
						_this.readonly = false;
					}
				}, 1000);
			},
			submit() {
				Comm.http('/api/service/login',{mobile:this.phone,code:this.code},'POST').then(res=>{
					if(res.code==1){
						uni.setStorageSync('serv_token',res.data);
						uni.navigateTo({
							url:'/'
						});
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">

	.login{
		    display: flex;
		    flex-direction: column;
		    padding: 28px 16px;
		.h1{
			color: #292929;
			font-size: 20px;
			font-weight: 700;
			margin: 30px 0;
			position: relative;
			z-index: 999;
			text-align: center;
		}
		.ipt{
			display: flex;
			align-items: center;
			padding: 8px;
			border-bottom: 0.5px solid rgba(0, 0, 0, 0.06);
			margin-bottom: 15px;
			position: relative;
			background: #f0f0f0;
			border-radius: 10px;
		}
		.ipt>input{
			width: auto;
			flex: 1;
		}
		.ipt>text{
			color: #38987c;
			font-size: 14px;
		}
		.btn{
			margin: 32px 0 0;
			height: 44px;
			background-color: #38987c;
			border-radius: 14px;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 16px;
		}
	}
</style>
